﻿Imports System
Imports System.Drawing
Imports System.Globalization
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class ColorMarkupSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Friend Overrides Function CanSerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            Return TypeOf value Is Color
        End Function

        Protected Friend Overrides Function DeserializeFromString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal propertyType As Type, ByVal value As String) As Object
            If propertyType.IsAssignableFrom(GetType(Color)) Then
                Dim str As String = value
                If Not String.IsNullOrEmpty(str) Then
                    If str.StartsWith("0X", StringComparison.OrdinalIgnoreCase) Then
                        Dim num As Long = (Convert.ToInt64(value, &H10) And CLng(&HFFFFFFFF))
                        Return Color.FromArgb(CByte((num >> &H18)), CByte((num >> &H10)), CByte((num >> 8)), CByte(num))
                    End If
                    Return MyBase.DeserializeFromString(serializationManager, propertyType, value)
                End If
            End If
            Return Nothing
        End Function

        Protected Friend Overrides Function SerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As String
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim str As String = String.Empty
            If TypeOf value Is Color Then
                Dim color As Color = DirectCast(value, Color)
                str = ("0X" & CLng(([CULng](((((color.A << &H18) Or (color.R << &H10)) Or (color.G << 8)) Or color.B)) And &HFFFFFFFF)).ToString("X08", CultureInfo.InvariantCulture))
            End If
            Return str
        End Function

    End Class
End Namespace

